package a07_二叉树;

import java.util.*;

/**
 * <p>
 * a05_二叉树的层序遍历复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/4/3
 */
public class a05_层序遍历01_二叉树的层序遍历复习3 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> resultList = new ArrayList<>();
        if (root == null) {
            return resultList;
        }

        Queue<TreeNode> stack = new LinkedList<>();
        stack.add(root);

        while (!stack.isEmpty()){
            List<Integer> result = new ArrayList<>();

            int size = stack.size();
            while (size > 0){
                TreeNode poll = stack.poll();
                result.add(poll.val);
                if(poll.left != null){
                    stack.add(poll.left);
                }
                if(poll.right != null){
                    stack.add(poll.right);
                }
                size--;
            }
            resultList.add(result);
        }

        return resultList;


    }
}
